<?php
/**
 * Created by PhpStorm.
 * User: meng
 * Date: 2023/10/10
 * Time: 10:16
 */

namespace app\admin\validator;

use lib\common\CommonValidator;

class MenuValidator extends CommonValidator
{
    protected $rule = [
        'id'           => 'require|postive',
        'menu_name'    => 'require|chsDash|length:2,32',
        'menu_symbol'  => 'require|alphaDash|length:2,64',
        'menu_icon'    => 'alphaDash|length:2,64',
        'menu_type'    => 'require|postive',
        'menu_affix'   => 'in:0,1',
        'menu_visible' => 'in:0,1',
        'menu_cache'   => 'in:0,1',
        'api_name'     => 'length:2,128',
        'api_path'     => 'length:2,128',
        'api_method'   => 'length:3,8|apiMethod',
        'list_order'   => 'long',
        'parent_id'    => 'require|long'
    ];

    protected $message = [
        'id.require'            => '参数中缺少ID',
        'id.postive'            => 'ID只能是正整数',
        'menu_name.require'     => '菜单名称必填',
        'menu_name.chsDash'     => '菜单名称只能为中文、英文、数字、下划_线及减号-',
        'menu_name.length'      => '菜单名称长度2-32字',
        'menu_symbol.require'   => '前端路由名称必填且只能为英文、数字、下划线及减号',
        'menu_symbol.alphaDash' => '前端路由名称必填且只能为英文、数字、下划线及减号',
        'menu_symbol.length'    => '前端路由名称长度2-64字',
        'menu_icon'             => '图标长度2-64字,只能为英文、数字、下划线及减号',
        'menu_type'             => '菜单类型必填只能为正整数',
        'menu_affix'            => '是否固定只能是0或1',
        'menu_visible'          => '菜单可见性只能是0或1',
        'menu_cache'            => '是否缓存只能是0或1',
        'api_name'              => '接口名称长度2-32字',
        'api_path'              => '接口路由长度2-128字',
        'api_method'            => '接口请求方式只能是GET/POST/PUT/DELETE',
        'list_order'            => '排序只能是数字',
        'parent_id'             => '父级菜单必填且只能是正整数'
    ];

    protected $scene = [
        'create' => ['menu_name', 'menu_symbol', 'menu_icon', 'menu_type', 'menu_affix', 'menu_visible', 'menu_cache', 'api_name', 'api_path', 'api_method', 'list_order', 'parent_id'],
        'update' => ['menu_name', 'menu_symbol', 'menu_icon', 'menu_type', 'menu_affix', 'menu_visible', 'menu_cache', 'api_name', 'api_path', 'api_method', 'list_order', 'parent_id']
    ];

    protected function apiMethod ($value): bool {
        return in_array($value, ['GET', 'POST', 'PUT', 'DELETE']);
    }
}
